package com.logos.ruralrevitalization.controller;

import com.google.common.base.Preconditions;
import com.logos.ruralrevitalization.req.ShoppingCartReq;
import com.logos.ruralrevitalization.resp.AgriculturalProductResp;
import com.logos.ruralrevitalization.result.Result;
import com.logos.ruralrevitalization.service.AgriculturalProductService;
import com.logos.ruralrevitalization.service.ShoppingCartService;
import com.logos.ruralrevitalization.utils.TouristLoginUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 农产品购物车表(ShoppingCart)表控制层
 *
 * @author rose
 * @since 2025-03-26 08:59:58
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Resource
    private ShoppingCartService shoppingCartService;

    @Resource
    private TouristLoginUtil touristLoginUtil;

    /**
     * 加入购物车
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody ShoppingCartReq shoppingCartReq) {
        try{
            if (log.isInfoEnabled()){
                log.info("ShoppingCartController.add.shoppingCartReq:{}", shoppingCartReq);
            }
            String id = touristLoginUtil.getId();
            if (StringUtils.isBlank(id)) {
                return Result.fail("请先登录");
            }
            Preconditions.checkNotNull(shoppingCartReq.getQuantity(),"数量不能为空");
            Preconditions.checkNotNull(shoppingCartReq.getProductId(),"农产品id不能为空");
            shoppingCartReq.setUserId(Long.valueOf(id));
            return Result.ok(shoppingCartService.add(shoppingCartReq));
        }catch(Exception e){
            log.error("ShoppingCartController.add.error:{}", e.getMessage(), e);
            return Result.fail("加入购物车失败");
        }
    }

    /**
     * 删除购物车中商品
     */
    @PostMapping("/deleteById")
    public Result<Boolean> delete(@RequestBody ShoppingCartReq shoppingCartReq) {
        try{
            if (log.isInfoEnabled()){
                log.info("ShoppingCartController.delete.shoppingCartReq:{}", shoppingCartReq);
            }
            String id = touristLoginUtil.getId();
            if (StringUtils.isBlank(id)) {
                return Result.fail("请先登录");
            }
            Preconditions.checkNotNull(shoppingCartReq.getId(),"当前购物车数据id不能为空");
            shoppingCartReq.setUserId(Long.valueOf(id));
            return Result.ok(shoppingCartService.deleteById(shoppingCartReq));
        }catch(Exception e){
            log.error("ShoppingCartController.delete.error:{}", e.getMessage(), e);
            return Result.fail("加入购物车失败");
        }
    }


    /**
     * 查询当前用户购物车
     */
    @GetMapping("/queryByUserId")
    public Result<List<AgriculturalProductResp>> queryByUserId() {
        try{
            String id = touristLoginUtil.getId();
            if (StringUtils.isBlank(id)) {
                return Result.fail("请先登录");
            }
            List<AgriculturalProductResp> result = shoppingCartService.queryByUserId(Long.valueOf(id));
            return Result.ok(result);
        }catch(Exception e){
            log.error("ShoppingCartController.queryByUserId.error:{}", e.getMessage(), e);
            return Result.fail("查询购物车失败");
        }
    }

    /**
     * 清空购物车
     */
    @PostMapping("/clear")
    public Result<Boolean> clear() {
        try{
            String id = touristLoginUtil.getId();
            if (StringUtils.isBlank(id)) {
                return Result.fail("请先登录");
            }
            return Result.ok(shoppingCartService.deleteAll(Long.valueOf(id)));
        }catch(Exception e){
            log.error("ShoppingCartController.clear.error:{}", e.getMessage(), e);
            return Result.fail("清空购物车失败");
        }
    }
}

